package com.drojian.screenAdaptation;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * 按照美工的基准值值生成真实设备上的宽高值
 */
public class UIUtils {
    //单例
    private static UIUtils instance;

    public static UIUtils getInstance() {
        return instance;
    }
    public static UIUtils getInstance(Context context) {
        if(instance == null){
            instance = new UIUtils(context);
        }
        return instance;
    }

    //标准宽高 （1920 - 48 = 1872 上面电量栏的高度）
    public static final int STANDARD_WIDTH = 1080;
    public static final int STANDARD_HEIGHT = 1872;
    public static final String  DIMEN_CLASS = "com.android.internal.R$dimen";
    //存储实际设备的宽高值
    public static float displayMetricsWidth;
    public static float displayMetricsHeight ;
    //初始化数据
    private UIUtils(Context context){
        //获取真实屏幕宽高
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        if(displayMetricsWidth == 0.0f || displayMetricsHeight == 0.0f){
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            //获取上面电量栏高度
            int systemBarHeight = getSystemBarHeight(context);
            //处理真实宽高(横屏  竖屏)
            if(displayMetrics.widthPixels > displayMetrics.heightPixels){
                this.displayMetricsWidth = (float)displayMetrics.heightPixels;
                this.displayMetricsHeight = (float) displayMetrics.widthPixels-systemBarHeight;
            } else if (displayMetrics.heightPixels >displayMetrics.widthPixels) {
                this.displayMetricsWidth = (float)displayMetrics.widthPixels;
                this.displayMetricsHeight = (float) displayMetrics.heightPixels;
            }
        }
    }

    private int getSystemBarHeight(Context context) {
        return getValue(context,"com.android.internal.R$dimen","system_bar_height",48);
    }

    /**
     *
     * @param context
     * @param attrGroupClass 安卓源码中找到存放围度得类
     * @param attrName 状态栏的信息
     * @param defValue 默认值
     * @return
     */

    private int getValue(Context context, String attrGroupClass, String attrName, int defValue) {
        try {
            Class<?> e = Class.forName(attrGroupClass);
            Object object = e.newInstance();
            Field field = e.getField(attrName);
            int x = Integer.parseInt(field.get(object).toString());
            return context.getResources().getDimensionPixelOffset(x);
        } catch (Exception e) {
            return 48;
        }

    }

    //开始获取缩放以后的结果
    public float getWidth(float width){
        return (width * this.displayMetricsWidth)/STANDARD_WIDTH;
    }
    public float getHeight(float height){
        return (height * this.displayMetricsHeight)/STANDARD_WIDTH;
    }
    public int getWidth(int width){
        return (int)((width * this.displayMetricsWidth)/STANDARD_WIDTH);
    }
    public int getHeight(int height){
        return (int)((height * this.displayMetricsHeight)/STANDARD_WIDTH);
    }
}
